<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>员工管理</title>
    <link rel="stylesheet" href="../../dist/css/bootstrap.min.css">
    <link href="../../css/bootstrap-datetimepicker.css" rel="stylesheet"/>
    <link rel="stylesheet" href="../../css/base.css">
    <link rel="stylesheet" href="../../css/all-like-orderlist.css">
    <link rel="stylesheet" href="../../css/people.css">
    <link rel="stylesheet" href="../../css/jquery.pagination.css">
    <script src="../../js/jquery.min.js"></script>
    <script src="../../dist/js/bootstrap.js"></script>
    <!--引入时间插件-->
    <script src="../../js/moment-with-locals.min.js"></script>
    <script src="../../js/bootstrap-datetimepicker.js"></script>
    <script>
        $(document).ready(function () {
            $("#datetimepicker").datetimepicker({
                format: "yyyymm",
                weekStart: 1,
                autoclose: true,
                startView: 3,
                minView: 3,
                forceParse: false,
                endDate: new Date(), // 窗口可选时间从今天开始
                pickerPosition: "bottom-left"
            });
            $("#datetimepicker2").datetimepicker({
                format: "yyyymm",
                weekStart: 1,
                autoclose: true,
                startView: 3,
                minView: 3,
                forceParse: false,
                endDate: new Date(), // 窗口可选时间从今天开始
                pickerPosition: "bottom-left"

            });
            //批量删除，获得批量编号
            $(".delbtn").click(function () {
                var ids = BBB('ck');
                if (ids.length > 0) {
                    $.ajax({
                        url: "/system/" + pid + "/deleteByIds",
                        contentType: "application/json; charset=utf-8",
                        type: "post",
                        data: ids,
                        success: function (data) {
                            if (data.code == "0") {
                                alert("删除成功!")
                                window.location.href = "/system/" + pid + "/salesman";
                            } else {
                                alert("删除失败!");
                            }
                        }
                    });
                }
            });

            var pid = $("#pid").val();
            $("#add_model").click(function () {
                $("#employee-name").val("");
                $("#employee-phone").val("");
                $("#addText").html("");

                if (pid == '2') {
                    $("#employee-pid").val('业务员');
                } else if (pid == '3') {
                    $("#employee-pid").val('清洗员');
                }
                //获取所属单位
                $.ajax({
                    url: "/system/" + pid + "/getPosition",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                    success: function (data) {
                        var html = "";
                        $.each(data, function (n, v) {
                            html += "<option>" + v + "</option>"
                        });
                        $("#bind_select").html(html);
                    }
                });
            });

            $("#add").click(function () {

                var employeeDto = {
                    name: $("#employee-name").val(),
                    phone: $("#employee-phone").val(),
                    unit: $("#bind_select option:selected").val()
                };

                $.ajax({
                    url: "/system/" + pid + "/addEmployee",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                    data: JSON.stringify(employeeDto),
                    success: function (data) {

                        if (data.code == "0") {
                            window.location.href = "/system/" + pid + "/salesman";
                        } else {
                            $("#addText").css("color", "red");
                            $("#addText").html(data.msg);
                        }
                    }
                });
            });
            $("#update").click(function () {
                var employeeDto = {
                    id: $("#update-id").val(),
                    name: $("#update-name").val(),
                    phone: $("#update-phone").val(),
                    unit: $("#update_bind_select option:selected").val(),
                };
                $.ajax({
                    url: "/system/" + pid + "/addEmployee",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                    data: JSON.stringify(employeeDto),
                    success: function (data) {

                        if (data.code == "0") {
                            window.location.href = "/system/" + pid + "/salesman";
                        } else {
                            $("#updateText").css("color", "red");
                            $("#updateText").html(data.msg);
                        }
                    }
                });
            });

            $("#query").click(function () {
                var employeePage = {
                    phone: $(".select-option .order-num").val(),
                    size: 5,
                };
                $.ajax({
                    url: "/system/" + pid + "/salesman",
                    contentType: "application/json; charset=utf-8",
                    type: "post",
                    data: JSON.stringify(employeePage),
                    success: function (data) {
                        $("#tb tr").html("");
                        var tb = "";
                        $.each(data.list, function (n, v) {
                            tb += "<tr id='tr'>" +
                                "<td><input name='ck'type='checkbox' value='" + v.id + "'/></td>" +
                                "<td>" + v.name + "</td>" +
                                "<td>" + v.phone + "</td>" +
                                "<td>" + v.unit + "</td>" +
                                "<td><img onclick='lick(this,this.id)' src='../../images/delete02.png'/><img src='../../images/edit.png'data-toggle=\"modal\"\n" +
                                "                            data-target='#exampleModal'" +
                                "                            onclick='updateClick(this, this.id)' " +
                                "                            data-whatever='修改员工信息'/></td>" +
                                "</tr>"

                        });
                        $("#tb").html(tb);
                        if ($("#tb").html()==""){
                            tb = "<tr><td colspan='5'>没有查询到数据!</td></tr>"
                            $("#tb").html(tb);
                            $("#tb tr td").css({"height":"100px","font-size":"20px","color":"red","text-align":"center","line-height":"100px"});
                        }
                    }
                });
            });
        })
    </script>
</head>
<body>
<div class="container-fluid">
    <div class="solid-hr row"></div>
    <div class="operation row">
        <button class="btn delete delbtn col-md-1"><img src="../../images/delete01.png" alt="">批量删除</button>
        <button id="add_model" data-toggle="modal" data-target="#addModal" class="btn btn-success distribute col-md-1">
            <img src="../../images/add.png" alt="">添加
        </button>
        <form class="select-option col-md-6">
            <input class="order-num" type="text" placeholder="请输入手机号">
            <input id="query" type="button" class="btn btn-success sub" value="查询">
        </form>
    </div>
    <input id="pid" type="hidden" th:value="${pid}"/>
    <!--表格-->
    <div class="table-list row">
        <table class="table table-bordered">
            <tr class="active">
                <td><input onclick="AAA('ck')" type="checkbox" checked></td>
                <td>员工姓名</td>
                <td>手机号</td>
                <td>所属单位</td>
                <td>操作</td>
            </tr>
            <tbody id="tb">
            <tr th:each="p : ${pagination.list}">
                <td><input name="ck" type="checkbox" th:value="${p.id}"></td>
                <td th:text="${p.name}">hys20180716</td>
                <td th:text="${p.phone}">张三</td>
                <td th:text="${p.unit}">245</td>
                <td><img onclick="lick(this, this.id)" src="../../images/delete02.png"/><img src="../../images/edit.png"
                                                                                             data-toggle="modal"
                                                                                             data-target="#exampleModal"
                                                                                             onclick="updateClick(this, this.id)"
                                                                                             data-whatever="修改员工信息"/>
                </td>
            </tr>
            </tbody>

        </table>
        <span th:text="${pagination.totalPage}" id="totalPage" style="display: none;"></span>
    </div>
    <!--分页-->
    <div class="page-right">
        <div id="pagination" class="page fl"></div>
    </div>

    <!--新增弹框-->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="addModalLabel">新增员工</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="employee-name" class="control-label"><i>*</i>员工姓名</label>
                            <input type="text" class="form-control" id="employee-name">
                        </div>
                        <div class="form-group">
                            <label for="employee-phone" class="control-label"><i>*</i>员工手机号</label>
                            <input type="text" class="form-control" id="employee-phone">
                        </div>
                        <div class="form-group">
                            <label for="employee-pid" class="control-label"><i>*</i>职位</label>
                            <input type="text" class="form-control" id="employee-pid" disabled="disabled">
                        </div>
                        <div class="form-group">
                            <label for="bind_select" class="control-label"><i>*</i>所在单位</label>
                            <select class="form-control" id="bind_select">

                            </select>
                        </div>
                        <span id="addText"></span>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default btn-333" data-dismiss="modal">取消</button>
                    <button id="add" type="button" class="btn btn-primary">确定</button>
                </div>
            </div>
        </div>
    </div>
    <!--修改弹框-->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">修改员工信息</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <input type="hidden" id="update-id"/>
                        <div class="form-group">
                            <label for="update-name" class="control-label"><i>*</i>员工姓名</label>
                            <input type="text" class="form-control" id="update-name">
                        </div>
                        <div class="form-group">
                            <label for="update-phone" class="control-label"><i>*</i>员工手机号</label>
                            <input type="text" class="form-control" id="update-phone">
                        </div>
                        <div class="form-group">
                            <label for="update_bind_select" class="control-label"><i>*</i>所属单位</label>
                            <select class="form-control" id="update_bind_select">
                                <option id="op1"></option>
                            </select>
                        </div>
                        <span id="updateText"></span>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="reset" class="btn btn-default btn-333" data-dismiss="modal">取消</button>
                    <button id="update" type="button" class="btn btn-primary">修改</button>
                </div>
            </div>
        </div>
    </div>
    <!--引入模态框和复选框js-->
    <script src="../../js/checkbox.js"></script>
    <script src="../../js/jquery.pagination.min.js"></script>
    <script>
        var pid = $("#pid").val();

        var totalPage = $("#totalPage").html();

        $("#pagination").pagination({
            totalPage: totalPage,
            isShow: true,
            count: totalPage,
            homePageText: "首页",
            endPageText: "尾页",
            prevPageText: "上一页",
            nextPageText: "下一页",
            callback: function (current) {
                var select_option = $(".select-option .order-num").val();
                var employeePage = {
                    currentPage : current,
                    phone: $(".select-option .order-num").val(),
                    size : 5,
                }

                if (select_option == "") {

                    $.ajax({
                        url: "/system/" + pid + "/salesman",
                        contentType: "application/json; charset=utf-8",
                        type: "post",
                        data: JSON.stringify(employeePage),
                        success: function (data) {
                            $("#tb tr").html("");
                            var tb = "";
                            $.each(data.list, function (n, v) {
                                console.log(v)
                                tb += "<tr id='tr'>" +
                                    "<td><input name='ck'type='checkbox' value='" + v.id + "'/></td>" +
                                    "<td>" + v.name + "</td>" +
                                    "<td>" + v.phone + "</td>" +
                                    "<td>" + v.unit + "</td>" +
                                    "<td><img onclick='lick(this,this.id)' src='../../images/delete02.png'/><img src='../../images/edit.png'data-toggle=\"modal\"\n" +
                                    "                            data-target='#exampleModal'" +
                                    "                            onclick='updateClick(this, this.id)' " +
                                    "                            data-whatever='修改员工信息'/></td>" +
                                    "</tr>"

                            });
                            $("#totalPage").html(data.totalPage);
                            $("#tb").html(tb)
                        }
                    });
                };
            },
        });

        function updateClick(obj, id) {

            $("#updateText").html("");
            var td_content = $(obj).parents("tr").children("td");  //获取当前行中的所有td值
            var id = td_content.eq(0).children("input").val(); //获取当前行第1个td的值
            var name = td_content.eq(1).text(); //获取当前行第2个td的值
            var phone = td_content.eq(2).text(); //获取当前行第2个td的值
            var unit = td_content.eq(3).text();

            console.log(unit)
            document.getElementById('update-id').value = id;
            document.getElementById('update-name').value = name;
            document.getElementById('update-phone').value = phone;
            document.getElementById('op1').value = unit;

            $.ajax({
                url: "/system/" + pid + "/getPosition",
                contentType: "application/json; charset=utf-8",
                type: "post",
                success: function (data) {

                    var html = "<option id='op1'>" + unit + "</option>";
                    $.each(data, function (n, v) {
                        html += "<option>" + v + "</option>"
                    })
                    $("#update_bind_select").html(html);
                }
            });

        }

        function lick(obj, id) {


            var td_content = $(obj).parents("tr").children("td");  //获取当前行中的所有td值
            var ids = td_content.eq(0).children("input").val(); //获取当前行第1个td的值
            console.log(ids)
            $.ajax({
                url: "/system/" + pid + "/deleteByIds",
                contentType: "application/json; charset=utf-8",
                type: "post",
                data: ids,
                success: function (data) {
                    if (data.code == "0") {
                        alert("删除成功!")
                        window.location.href = "/system/" + pid + "/salesman";
                    } else {
                        alert("删除失败!");
                    }
                }
            });

        }
    </script>
</div>
</body>
</html>